package com.xudow.app.dynamicstate_old.module.user.viewholder;

import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.domain.entity.UserCourseWithOtherInfo;
import com.xudow.app.ui.widget.StrikethroughTextView;
import com.xudow.app.util.LngLatUtil;
import com.xudow.app.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ske on 2016/6/11.
 */
public class PreferredCourseViewHolder extends BaseViewHolder<UserCourseWithOtherInfo> {

    @BindView(R.id.agency_img)
    RoundedImageView agencyImg;
    @BindView(R.id.course_name)
    TextView courseName;
    @BindView(R.id.agency_name)
    TextView textAgencyName;
    @BindView(R.id.course_name_ll)
    LinearLayout courseNameLl;
    @BindView(R.id.course_type)
    TextView courseType;
    @BindView(R.id.address)
    TextView textAddress;
    @BindView(R.id.distance)
    TextView textDistance;
    @BindView(R.id.actual_price)
    TextView actualPrice;
    @BindView(R.id.real_price)
    StrikethroughTextView realPrice;
    @BindView(R.id.info_bg)
    LinearLayout infoBg;
    @BindView(R.id.detail_item)
    RelativeLayout detailItem;

    public PreferredCourseViewHolder(ViewGroup parent) {
        super(parent, R.layout.item_recomment_preferred_course);
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(UserCourseWithOtherInfo course) {
        courseName.setText(course.getName());
        String agencyName = "";
        if (StringUtils.isEmpty(course.getAgencyName()) && !StringUtils.isEmpty(course.getTeacher()))
            agencyName = course.getTeacher();
        else
            agencyName = course.getAgencyName();
        textAgencyName.setText("(" + agencyName + ")");
        if (course.getType() == 1)
            courseType.setText("一对一");
        else if (course.getType() == 2)
            courseType.setText("辅导班");
        realPrice.setText("￥" + course.getCoursePrice());
        actualPrice.setText("￥" + course.getActualPrice());
        String address = "";
        if (!StringUtils.isEmpty(course.getSchoolName()))
            address = course.getSchoolName();

        textAddress.setText(address);

        String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, course.getThumbnail());
        Picasso.with(getContext()).load(imageUrl).placeholder(R.mipmap.course_def_img).into(agencyImg);

        //计算距离
        XApplication application = XApplication.getInstance();
        if (null != application && null != course.getLongtitude() && null != course.getLatitude() &&
                null != application.longitude && null != application.latitude) {
            String distance = LngLatUtil.distanceFormatted(course.getLongtitude().doubleValue(), course.getLatitude().doubleValue(),
                    application.longitude, application.latitude);

            if (!StringUtils.isEmpty(distance)) {
                textDistance.setText(distance);
            }
        }
    }
}
